package com.gxuwz.endrskyweather.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.recyclerview.widget.RecyclerView;

import com.gxuwz.endrskyweather.R;
import com.gxuwz.endrskyweather.model.City;
import com.gxuwz.endrskyweather.viewmodel.CityManagerViewModel;
import com.gxuwz.endrskyweather.viewmodel.FavoriteViewModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class CityManagerListAdapter extends RecyclerView.Adapter<CityManagerListAdapter.CityViewHolder> {

    private List<City> cities;

    public CityManagerListAdapter(List<City> cities) {
        this.cities = cities;
    }

    @NonNull
    @Override
    public CityViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_favorite_city, parent, false);
        return new CityViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CityViewHolder holder, int position) {
        City city = cities.get(position);
        holder.bind(city);
    }

    @Override
    public int getItemCount() {
        return cities != null ? cities.size() : 0;
    }

    public void updateData(List<City> newCities) {
        if (cities == null) {
            cities = new ArrayList<>();
        }
        cities.clear();
        cities.addAll(newCities);
        notifyDataSetChanged();
    }

    static class CityViewHolder extends RecyclerView.ViewHolder {
        TextView tvCityName;
        TextView tvCityCode;
        ImageButton ibSetDefault;
        ImageButton ibDelete;

        public CityViewHolder(@NonNull View itemView) {
            super(itemView);
            tvCityName = itemView.findViewById(R.id.tv_city_name);
            tvCityCode = itemView.findViewById(R.id.tv_city_code);
            ibSetDefault = itemView.findViewById(R.id.btn_set_default);
            ibDelete = itemView.findViewById(R.id.btn_delete);
        }

        public void bind(City city) {
            tvCityName.setText(city.getAreaCn());
            tvCityCode.setText(city.getAreaCode());

            ibSetDefault.setOnClickListener(v -> {
                CityManagerViewModel viewModel = new ViewModelProvider(
                        (ViewModelStoreOwner) v.getContext(),
                        ViewModelProvider.AndroidViewModelFactory.getInstance(
                                ((Activity) v.getContext()).getApplication()
                        )
                ).get(CityManagerViewModel.class);

                viewModel.setDefaultCity(city);
                Toast.makeText(v.getContext(), "已设为默认城市", Toast.LENGTH_SHORT).show();
            });

            ibDelete.setOnClickListener(v -> {
                CityManagerViewModel viewModel = new ViewModelProvider(
                        (ViewModelStoreOwner) v.getContext(),
                        ViewModelProvider.AndroidViewModelFactory.getInstance(
                                ((Activity) v.getContext()).getApplication()
                        )
                ).get(CityManagerViewModel.class);

                viewModel.deleteCity(city);
                viewModel.getDeleteFlag().observe((LifecycleOwner) v.getContext(), result -> {
                    if (result) {
                        Toast.makeText(v.getContext(), "删除成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(v.getContext(), "删除失败", Toast.LENGTH_SHORT).show();
                    }
                });
            });
        }

    }
}
